package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {
    @Delete("delete from emp where id=#{id}")
    void delete(Integer id);

    //后面的都是对象里面的属性，要换成驼峰命名法
    //useGeneratedKeys = true意思是返回主键，keyProperty = "id"的意思是把主键的值赋值给对象id属性
    @Options(keyProperty = "id",useGeneratedKeys = true)
    @Insert("insert into emp(username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) " + "values (#{username},#{password},#{name}, #{gender}, #{image},#{job},#{entrydate}, #{deptId}, #{createTime},#{updateTime})")
    void insert(Emp emp);


    //这里需要id，这个id是你要修改的人的主键值，不是对象的id值
    @Update(" update emp set username=#{username}, name=#{name}, gender=#{gender},image=#{image}, job=#{job}, entrydate=#{entrydate}, dept_id=#{deptId}, update_time=#{updateTime} where id=#{id}")
    void update(Emp emp);


    /*@Select("select * from emp where id=#{id}")
    /*方案一，给字段起别名，让别名和实体类属性名称一致
    @Select("select id, username, password, name, gender, image, job, entrydate, " +
            "dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{id}")
    */
    /*方案二，通过 @Result,@Results注解手动映射封装
    @Results ({
            @Result(column = "dept_id",property = "deptId"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from emp where id=#{id}")*/

    //方案三，开启mybatis驼峰命名设备
    @Select("select * from emp where id=#{id}")
    Emp getById(Integer id);

    //条件查询
    //不好传入对象，索性直接一个个传入参数
    //%#{nane}%
    //模糊查询切记,字符串，#{}最后生成预编译的sql，要被?代替，?参数占位符不可以出现在‘’之内，
    // 所以不可以使用#{}，可以用${},$字符串拼接符号,不会生成预编译sql，会将张和两个%%字符串拼接

    //但是应为不用预编译sql，出现sql注入问题等，所以可以用拼接函数
    /*@Select("select *from emp where name like '%${nane}%' and gender=#{gender} and entrydate" +
            " between #{begin} and #{end}  order by update_time desc;")*/

    /*@Select("select *from emp where name like concat('%',#{name},'%') and gender=#{gender} and entrydate" +
            " between #{begin} and #{end}  order by update_time desc;")*/
    List<Emp> list(@Param("name") String nane, Short gender, LocalDate begin,LocalDate end);


    void update2(Emp emp);
    //方法名称里面带有update就会在xml文件中生成update标签


    void deleteByIds(List<Integer> ids);
}
